﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace UserLoginDemo.DAl
{
    public class SqlHelper
    {
        //1.连接字符串
        private static string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;

        //2.执行增删改的
        public static int ExecuteNonQuery(string sql, params SqlParameter[] pms)//返回受影响的行数
        {
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand(sql, con))
                {
                    if (pms != null)
                    {
                        cmd.Parameters.AddRange(pms);
                    }
                    con.Open();
                    return cmd.ExecuteNonQuery();
                }
            }

        }

        //3.执行返回单个值的
        public static object ExecuteScalar(string sql, params SqlParameter[] pms)
        {//返回一个首行首列
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand(sql, con))
                {
                    if (pms != null)
                    {
                        cmd.Parameters.AddRange(pms);
                    }
                    con.Open();
                    return cmd.ExecuteScalar();
                }
            }
        }

        //4.执行返回SqlDataReader
        public static SqlDataReader ExecuteReader(string sql, params SqlParameter[] pms)
        {
            //返回一个结果集
            SqlConnection con = new SqlConnection(constr);
            using (SqlCommand cmd = new SqlCommand(sql, con))
            {
                if (pms != null)
                {
                    cmd.Parameters.AddRange(pms);
                }
                try
                {
                    con.Open();
                    return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
                }
                catch (Exception)
                {
                    con.Close();
                    con.Dispose();
                    throw;
                }
            }
        }

        public static DataTable ExecuteDataTable(string sql, params SqlParameter[] pms)
        {
            DataTable dt = new DataTable();
            using (SqlDataAdapter adapter = new SqlDataAdapter(sql, constr))//创建适配器
            {
                if (pms != null)
                {
                    adapter.SelectCommand.Parameters.AddRange(pms);
                }
                adapter.Fill(dt);
            }
            return dt;
        }

        public static DataTable GetDataTable(string sql, CommandType type, params SqlParameter[] pars)
        {
            using (SqlConnection conn = new SqlConnection(constr))
            {
                using (SqlDataAdapter adpter = new SqlDataAdapter(sql, conn))
                {
                    if (pars != null)
                    {
                        adpter.SelectCommand.Parameters.AddRange(pars);
                    }
                    adpter.SelectCommand.CommandType = type;
                    DataTable da = new DataTable();
                    adpter.Fill(da);
                    return da;
                }
            }
        }

    }
}
